import * as types from '../constants/ActionTypes';
import Request from '../utils/request';

/**
*详情页数据请求
*/
const fetchDetail = (id, cb) => (dispatch) => {
  Request.get({
    url: `/dealer/mycustomers_fav/${id}?app=1`,
    success: (data) => {
      if (cb) {
        cb();
      }
      dispatch(details(data));
    },
    fail: (err) => {
      console.log(err);
      dispatch(details({}));
      if (cb) {
        cb({
          err: '失败',
        });
      }
    },
    error: (err) => {
      console.log(err);
      dispatch(details({}));
      if (cb) {
        cb({
          err: '失败',
        });
      }
    },
  });
};

const fetchMoreVisit = (id, callback) => (dispatch) => {
  Request.get({
    url: `/dealer/remind/${id}`,
    success: (visit) => {
      dispatch(moreVisit(visit));
      if (callback) {
        callback();
      }
    },
    fail: () => {
      console.log('fail');
      dispatch(moreVisit({}));
    },
    error: () => {
      console.log('error');
      dispatch(moreVisit({}));
    },
  });
};

const fetchMoreVoices = (id, callback) => (dispatch) => {
  Request.get({
    url: `/dealer/more/voicemsg/${id}`,
    success: (voices) => {
      console.log('fetchMoreVoices-success');
      dispatch(getMoreVoices(voices));
      if (callback) {
        callback();
      }
    },
    fail: () => {
      console.log('fail');
      dispatch(getMoreVoices({}));
    },
    error: () => {
      console.log('error');
      dispatch(getMoreVoices({}));
    },
  });
};

const fetchNote = id => (dispatch) => {
  Request.get({
    url: `/dealer/note/${id}`,
    success: (note) => {
      dispatch(getNotes(note));
      console.log('获取备注成功！');
    },
    fail: () => {
      dispatch(getNotes({}));
      console.log('获取备注失败！');
    },
    error: () => {
      dispatch(getNotes({}));
      console.log('获取备注错误！');
    },
  });
};

const changeNote = (note, id, callback) => (dispatch) => {
  Request.post({
    url: `/dealer/note/${id}`,
    data: {
      note,
    },
    success: () => {
      dispatch(chNote(note));
      console.log('修改备注成功！');
      if (callback) {
        callback();
      }
    },
    fail: () => {
      dispatch(chNote(''));
      console.log('修改备注失败！');
    },
    error: () => {
      dispatch(chNote(''));
      console.log('修改备注错误！');
    },
  });
};

const changeVisitReminder = (cVisitRemind, id, cb) => (dispatch) => {
  Request.put({
    url: `/dealer/remind/${id}`,
    contenttype: 'form',
    data: cVisitRemind,
    success: () => {
      dispatch(changeVisitRemind(cVisitRemind));
      console.log('修改拜访提醒成功！', cVisitRemind);
      if (cb) {
        cb();
      }
    },
    fail: () => {
      dispatch(changeVisitRemind({}));
      console.log('修改拜访提醒失败！');
    },
    error: () => {
      dispatch(changeVisitRemind({}));
      console.log('修改拜访提醒错误！');
    },
  });
};

const addVisitReminder = (aVisitRemind, id, cb) => (dispatch) => {
  Request.post({
    url: `/dealer/remind/${id}`,
    data: aVisitRemind,
    success: () => {
      dispatch(addVisitRemind(aVisitRemind));
      console.log('添加拜访提醒成功！');
      if (cb) {
        cb();
      }
    },
    fail: () => {
      dispatch(addVisitRemind({}));
      console.log('添加拜访提醒失败！');
    },
    error: () => {
      dispatch(addVisitRemind({}));
      console.log('添加拜访提醒错误！');
    },
  });
};

const sendIntentionFeedback = (data, uid, callback) => (dispatch) => {
  Request.post({
    url: `/dealer/intention_feedback/${uid}`,
    data,
    success: () => {
      dispatch(postFeedback(data));
      if (callback) {
        callback();
      }
      console.log('购买意向发送成功！');
    },
    fail: () => {
      dispatch(postFeedback({}));
      console.log('购买意向发送失败！');
    },
    error: () => {
      dispatch(postFeedback({}));
      console.log('购买意向发送错误！');
    },
  });
};

// 用户阶段
const userStageSelect = (stage, id) => (dispatch) => {
  Request.post({
    url: `/dealer/users/stage/${id}`,
    data: {
      stage,
    },
    success: () => {
      dispatch(stageSelect(stage));
      console.log('选择用户阶段成功！');
    },
    fail: () => {
      dispatch(stageSelect(''));
      console.log('选择用户阶段失败！');
    },
    error: () => {
      dispatch(stageSelect(''));
      console.log('选择用户阶段错误！');
    },
  });
};

const deleteVisitReminder = (uid, vid, callback) => (dispatch) => {
  Request.delete({
    url: `/dealer/remind/${uid}`,
    contenttype: 'form',
    data: {
      id: vid,
    },
    success: () => {
      dispatch(deleteVisitRemind(vid));
      console.log('删除拜访提醒成功！');
      if (callback) {
        callback();
      }
    },
    fail: () => {
      dispatch(deleteVisitRemind(''));
      console.log('删除拜访提醒失败！');
    },
    error: () => {
      dispatch(deleteVisitRemind(''));
      console.log('删除拜访提醒错误！');
    },
  });
};

const details = data => ({
  type: types.MY_CUSTOMER_DETAILS,
  data,
});

const chNote = note => ({
  type: types.CHANGE_NOTE,
  note,
});

const getNotes = getNote => ({
  type: types.GET_NOTE,
  getNote,
});

const moreVisit = visit => ({
  type: types.MORE_VISIT,
  visit,
});

const changeVisitRemind = cVisitRemind => ({
  type: types.CHANGE_VISIT_REMIND,
  cVisitRemind,
});

const addVisitRemind = aVisitRemind => ({
  type: types.ADD_VISIT_REMIND,
  aVisitRemind,
});

const stageSelect = stage => ({
  type: types.USER_STAGE_SELECT,
  stage,
});

const deleteVisitRemind = vid => ({
  type: types.DELETE_VISIT_REMIND,
  vid,
});

const postFeedback = feedback => ({
  type: types.FEEDBACK,
  feedback,
});

const getMoreVoices = moreVoices => ({
  type: types.VOICES,
  moreVoices,
});

export {
  fetchDetail,
  changeNote,
  fetchNote,
  fetchMoreVisit,
  fetchMoreVoices,
  changeVisitReminder,
  addVisitReminder,
  deleteVisitReminder,
  userStageSelect,
  sendIntentionFeedback,
};
